package com.frejoys.store.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.aop.VerifyStore;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.StoreActivityAddDto;
import com.frejoys.common.dto.admin.StoreActivityUpDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.store.StoreActivityVo;
import com.frejoys.dao.entity.Store;
import com.frejoys.dao.entity.StoreActivity;
import com.frejoys.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "店铺活动", description = "商家端店铺活动管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixStore)
@Authorization(Authority.Private)
public class MallStoreActivityController {

    private final StoreService storeService;

    private final StoreActivityService storeActivityService;

    private final RedissonClient redissonClient;

    @Operation(summary = "活动列表")
    @PostMapping("/storeActivity/list")
    public Result<List<StoreActivityVo>> activityList(@RequestBody @Validated IdDto dto) {
        LambdaQueryWrapper<StoreActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreActivity::getStoreId, dto.getId());
        List<StoreActivity> activities = storeActivityService.getList(queryWrapper);
        List<StoreActivityVo> activityVos = BeanUtil.copyToList(activities, StoreActivityVo.class);
        return Result.success(activityVos);
    }

    @Operation(summary = "活动详情")
    @PostMapping("/storeActivity/detail")
    public Result<StoreActivityVo> detail(@RequestBody @Validated IdDto dto) {
        StoreActivity activity = storeActivityService.getById(dto.getId());
        Store store = storeService.getOnly(Wrappers.<Store>lambdaQuery().select(Store::getName).eq(Store::getId, activity.getStoreId()));
        StoreActivityVo vo = BeanUtil.copyProperties(activity, StoreActivityVo.class);
        vo.setStoreName(store.getName());
        return Result.success(vo);
    }

    @VerifyStore
    @Operation(summary = "添加活动")
    @PostMapping("/storeActivity/addInfo")
    public Result offShelve(@RequestBody @Validated StoreActivityAddDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.storeActivityAddLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeActivityService.addInfo(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }


        return Result.success();
    }

    @VerifyStore
    @Operation(summary = "编辑活动")
    @PostMapping("/storeActivity/upInfo")
    public Result offShelve(@RequestBody @Validated StoreActivityUpDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.upStoreActivityLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeActivityService.upInfo(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


}
